#include <string>
#include <iostream>
#include <sstream>
#include "parseur.hpp"

Parseur::Parseur()
{
}

void			Parseur::setFile(const std::string& file)
{
  this->_fileName = file;
  this->_fd.open(file.c_str(), std::ifstream::in);
}

bool			Parseur::parseLine(ObjManager* oman)
{
  static unsigned int  	size_max = 1024;
  static char*		buf = new char[size_max];
  static unsigned int	line_num = 0;
  std::string		sign;
  unsigned int		cpt = 0;
  AObject*		tmp_obj;

  sign.reserve(1024);
  if (!this->_fd.good())
    return false;
  this->_fd.getline(buf, size_max);
  std::cout << "line" << line_num << ": " << buf << std::endl;

  while (buf[cpt] != '\0')
    {
      tmp_obj = NULL;
      while ((oman->isOp(buf[cpt]) == ObjManager::UNDEFINED) && (buf[cpt] != '\0'))
	{
	  sign += buf[cpt];
	  ++cpt;
	}
      if (sign.size() != 0)
	{
	  oman->createFact(sign);
	  tmp_obj = oman->getFact(sign);
	  sign.clear();
	}
      else
	{
	  tmp_obj = oman->createOperator(oman->isOp(buf[cpt]));
	  ++cpt;
	}
      if (tmp_obj != NULL)
	oman->addObj(line_num, tmp_obj);
    }
  ++line_num;
  
  return true;
}
